package leetcode.code1537;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

/**
 *
 * 【瓦片】1537. 最大得分 - Java 100% - 双针
 *
 * @author CaoYu
 * @date 2021年12月25日
 * @description
 */
public class Solution implements IDebug {

	int MOD = (int) 1e9 + 7;

	public int maxSum(int[] nums1, int[] nums2) {
		long ans = 0;// 用int泪如雨下。懂得自然懂。
		int len1 = nums1.length;
		int len2 = nums2.length;
		int p1 = len1 - 1;
		int p2 = len2 - 1;
		long sum1 = 0;// 用int泪如雨下。懂得自然懂。
		long sum2 = 0;// 用int泪如雨下。懂得自然懂。
		while (p1 >= 0 && p2 >= 0) {
			int num1 = nums1[p1];
			int num2 = nums2[p2];
			if (num1 == num2) {
				ans = (ans + Math.max(sum1, sum2) + num1) % MOD;
				while (p1 >= 0 && nums1[p1] == num1) {
					p1--;
				}
				while (p2 >= 0 && nums2[p2] == num2) {
					p2--;
				}
				sum1 = 0;
				sum2 = 0;
			} else if (num1 > num2) {
				sum1 += num1;
				p1--;
			} else {
				sum2 += num2;
				p2--;
			}
		}
		while (p1 >= 0) {
			sum1 += nums1[p1--];
		}
		while (p2 >= 0) {
			sum2 += nums2[p2--];
		}
		ans += Math.max(sum1, sum2) % MOD;
		return (int) ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(30, this.maxSum(HelpDebug.str2array3("[2,4,5,8,10]"), HelpDebug.str2array3("[4,6,8,9]")));
	}

	@Override
	public void debug2() {
		HelpDebug.compare(109, this.maxSum(HelpDebug.str2array3("[1,3,5,7,9]"), HelpDebug.str2array3("[3,5,100]")));
	}

	@Override
	public void debug3() {
		HelpDebug.compare(40, this.maxSum(HelpDebug.str2array3("[1,2,3,4,5]"), HelpDebug.str2array3("[6,7,8,9,10]")));
	}

	@Override
	public void debug4() {
		HelpDebug.compare(61, this.maxSum(HelpDebug.str2array3("[1,4,5,8,9,11,19]"), HelpDebug.str2array3("[2,3,4,11,12]")));
	}

	public void debug5() {
		HelpDebug.compare(61, this.maxSum(HelpDebug.str2array3("[1,4,5,8,9,11,19]"), HelpDebug.str2array3("[2,3,4,11,12]")));
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
